package tankgame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;

public class myPanel extends JPanel implements KeyListener,Runnable {
    Hero hero = null;
    Image image1 = null;
    Image image2 = null;
    Image image3 = null;
    Vector<Bomb> bombs = new Vector<>();
    static Vector<enemyTank> enemyTanks = new Vector<>();
    int initnum_enmytank = 3;

    public myPanel() {
        hero = new Hero(700, 100, 0);
        for (int i = 0; i < initnum_enmytank; i++) {
            enemyTank enemyTank = new enemyTank(100 * (i + 1), 0, 2);
            enemyTanks.add(enemyTank);
            new Thread(enemyTank).start();
        }
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.fillRect(0, 0, 1000, 750);
        if(hero.getIs_alive()){
            drawTank(hero.getX(), hero.getY(), g, hero.getDirection(), 0);
        }
        for (int i = 0; i < enemyTanks.size(); i++) {
            enemyTank enemyTank = enemyTanks.get(i);
            if(enemyTank.getIs_alive()) {
                drawTank(enemyTank.getX(), enemyTank.getY(), g, enemyTank.getDirection(), 1);
                for (int j = 0; j <enemyTank.Shots.size(); j++) {
                    Shot shot = enemyTank.Shots.get(j);
                    if (shot.is_alive) {
                        g.fillRect(shot.getX(), shot.getY(), 2, 2);
                    }else{
                        enemyTank.Shots.remove(shot);
                    }
                }
            }else{
                enemyTanks.remove(enemyTank);
            }
        }
        image1 = Toolkit.getDefaultToolkit().getImage(myPanel.class.getResource("/1.png"));
        image2 = Toolkit.getDefaultToolkit().getImage(myPanel.class.getResource("/2.png"));
        image3 = Toolkit.getDefaultToolkit().getImage(myPanel.class.getResource("/3.png"));

        for (int i = 0; i < bombs.size(); i++) {
            Bomb bomb = bombs.get(i);
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            if(bomb.life > 6){
                g.drawImage(image3,bomb.x,bomb.y,60,60,this);
            }else if(bomb.life > 3){
                g.drawImage(image2,bomb.x,bomb.y,60,60,this);
            }else if(bomb.life > 0){
                g.drawImage(image1,bomb.x,bomb.y,60,60,this);
            }else if(bomb.life == 0){
               bombs.remove(bomb);
            }
            bomb.lifeDown();

        }
        for (Shot shot : hero.shots) {
            if (shot.is_alive) {
                g.fillRect(shot.getX(), shot.getY(), 2, 2);
            }else{
                hero.shots.remove(shot);
            }
        }
    }





public void drawTank(int x, int y, Graphics g, int direction, int type) {
    switch (type) {
        case 0:
            g.setColor(Color.CYAN);
            break;
        case 1:
            g.setColor(Color.yellow);
    }
    switch (direction) {
        case 0://表示坦克向上
                g.fill3DRect(x, y, 10, 60, false);
                g.fill3DRect(x + 10, y + 10, 20, 40, false);
                g.fill3DRect(x + 30, y, 10, 60, false);
                g.fillOval(x + 10, y + 20, 20, 20);
                g.drawLine(x + 20, y, x + 20, y + 20);
                break;
        case 1:
                g.fill3DRect(x, y, 60, 10, false);
                g.fill3DRect(x + 10, y + 10, 40, 20, false);
                g.fill3DRect(x, y + 30, 60, 10, false);
                g.fillOval(x + 20, y + 10, 20, 20);
                g.drawLine(x + 40, y + 20, x + 60, y + 20);
                break;
        case 2:
                g.fill3DRect(x, y, 10, 60, false);
                g.fill3DRect(x + 10, y + 10, 20, 40, false);
                g.fill3DRect(x + 30, y, 10, 60, false);
                g.fillOval(x + 10, y + 20, 20, 20);
                g.drawLine(x + 20, y + 40, x + 20, y + 60);
                break;
        case 3:
                g.fill3DRect(x, y, 60, 10, false);
                g.fill3DRect(x + 10, y + 10, 40, 20, false);
                g.fill3DRect(x, y + 30, 60, 10, false);
                g.fillOval(x + 20, y + 10, 20, 20);
                g.drawLine(x, y + 20, x + 20, y + 20);
                break;
    }

}

@Override
public void keyTyped(KeyEvent e) {

}

@Override
public void keyPressed(KeyEvent e) {
    if (e.getKeyCode() == KeyEvent.VK_W) {
        if(hero.getY() > 0){
            hero.setDirection(0);
            hero.moveUp();
        }
    } else if (e.getKeyCode() == KeyEvent.VK_A) {
        if(hero.getX() > 0){
            hero.setDirection(3);
            hero.moveLeft();
        }
    } else if (e.getKeyCode() == KeyEvent.VK_S) {
        if(hero.getY() + 60 < 750){
            hero.setDirection(2);
            hero.moveDown();
        }
    } else if (e.getKeyCode() == KeyEvent.VK_D) {
        if(hero.getX() + 60 < 1000){
            hero.setDirection(1);
            hero.moveRight();
        }
    } else if (e.getKeyCode() == KeyEvent.VK_J) {
        if(hero.shots.size() < hero.MAX_SHOT_NUM){
            hero.shotEnemy();
        }


    }
}

    @Override
    public void keyReleased(KeyEvent e) {

    }
    public void hitTank(Shot shot,tank tank){
        if(shot.is_alive){
            switch(tank.getDirection()){
                case 0:
                case 2:
                    if(shot.getX() > tank.getX() && shot.getX() < tank.getX() + 40
                    && shot.getY() > tank.getY() && shot.getY() < tank.getY() + 60){
                        shot.is_alive = false;
                        tank.setIs_alive(false);
                        bombs.add(new Bomb(tank.getX() ,tank.getY()));

                    }
                case 1:
                case 3:
                    if(shot.getX() > tank.getX() && shot.getX() < tank.getX() + 60
                            && shot.getY() > tank.getY() && shot.getY() < tank.getY() + 40){
                        shot.is_alive = false;
                        tank.setIs_alive(false);
                        bombs.add(new Bomb(tank.getX(),tank.getY()));
                    }
            }
        }
    }
    @Override
    public void run() {
        while(true){
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            //每隔一段时间判断子弹是否打中了坦克
            for (Shot shot : hero.shots) {
                if(shot.is_alive){
                    for (int i = 0; i < enemyTanks.size(); i++) {
                        enemyTank et = enemyTanks.get(i);
                        if(et.getIs_alive()){
                            hitTank(shot,et);
                        }
                    }
                }
            }

            for (int i = 0; i < enemyTanks.size(); i++) {
                enemyTank et = enemyTanks.get(i);
                for (int j = 0; j < et.Shots.size(); j++) {
                    Shot shot = et.Shots.get(j);
                    if(shot.is_alive && hero.getIs_alive()){
                        hitTank(shot,hero);
                    }
                }

            }

            this.repaint();
        }

    }
}

